import json

from odoo import http
from odoo.http import request


class CommonController(http.Controller):

    @http.route('/get/fileUrl', type='http', auth='user', methods=['GET'])
    def get_file_url(self, id):
        file = request.env['ir.attachment'].sudo().search([('id', '=', id)])
        base_url: str = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
        base_url = base_url.replace('https', 'http')
        url = base_url + file.local_url
        data = json.dumps({
            'url': url,
            'preview_url': url
        })
        return data
